package com.stars.home.vmodel

import android.app.Activity
import android.app.Application
import android.text.TextUtils
import androidx.databinding.Bindable
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.hwangjr.rxbus.RxBus
import com.stars.home.BR
import com.stars.home.config.ConfigMMKV
import com.stars.home.config.ConfigRx
import com.stars.home.model.User
import com.stars.home.utils.net.NetEngine
import com.trailer.base.base.BRepository
import com.trailer.base.base.BViewModel
import com.trailer.base.utils.MMKVManager
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.launch
import java.util.concurrent.TimeUnit

class WithdrawVModel(application: Application) : BViewModel<BRepository>(application) {

   // var image: MutableLiveData<String> = MutableLiveData("")
   var image=""

    var codeTip: MutableLiveData<String> = MutableLiveData(getCodeStr)
    private var disposable: Disposable? = null

    var phone: MutableLiveData<String> = MutableLiveData()


    var one= MutableLiveData<Boolean>(true)
    var two= MutableLiveData<Boolean>(false)

    var type="alipay"//alipay=支付宝,wechat=微信


    companion object {
        const val time = 60
        var timeStart: Long = 1
        const val getCodeStr = "获取验证码"

    }


    fun toChose( chose:Int){
        when(chose){
            1->{
                type="alipay"
                one.value=true
                two.value=false
            }
            2->{
                type="wechat"
                one.value=false
                two.value=true
            }
        }
    }


    @Bindable
    open var amount: String? =null
        set(value) {
            field = value
            notifyPropertyChanged(BR.amount)
        }

    @Bindable
    open var number: String? =null
        set(value) {
            field = value
            notifyPropertyChanged(BR.number)
        }

    @Bindable
    open var name: String? =null
        set(value) {
            field = value
            notifyPropertyChanged(BR.name)
        }

    @Bindable
    open var code: String? =null
        set(value) {
            field = value
            notifyPropertyChanged(BR.code)
        }


    fun setToSubmit(){

        if(TextUtils.isEmpty(amount)){
            toast("请输入提现金额（单位：元）")
            return
        }
        if(TextUtils.isEmpty(number)){
            toast("请输入您的支付宝或微信账号")
            return
        }
        if(TextUtils.isEmpty(name)){
            toast("请输入您的真实姓名")
            return
        }
        if(TextUtils.isEmpty(code)){
            toast("请输入验证码")
            return
        }

        viewModelScope.launch {
           var it= repository().lreq { NetEngine.service.withdrawal(amount,type,number,name,phone.value,code) }
            if(it.ok(true)){

                RxBus.get().post(ConfigRx.REFRESH_MONEY, "")

                var con = repository().context as Activity
                con.finish()
            }
        }



    }




    fun getSmsCode(start: Long = 1) {
        if (codeTip.value != ForgetPasswordVModel.getCodeStr) {
            return
        }

        if (phone.value.isNullOrEmpty()) {
            toast("没有注册的手机号没法提现！")
            return
        }


        viewModelScope.launch {
            val it = repository().lreq { NetEngine.service.getPassword(phone.value,"withdraw","1") }
            if (it.ok(true)) {
                startTimer(start)
            }

        }

    }
    private fun startTimer(start: Long = 1) {
        disposeTimer()
        disposable = Observable.intervalRange(start, ForgetPasswordVModel.time - start + 1, 0, 1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext {
                    ForgetPasswordVModel.timeStart = it
                    codeTip.value = if (ForgetPasswordVModel.time - it > 0) "${ForgetPasswordVModel.time - it}秒后重发" else {
                        ForgetPasswordVModel.getCodeStr
                    }
                }
                .doOnComplete() {
                    MMKVManager.instance().encode(ConfigMMKV.REGISTER_TIME_START, 60)
                }
                .doOnDispose() {
                    MMKVManager.instance().encode(ConfigMMKV.REGISTER_TIME_START, 60)
                }
                .subscribe()
    }

    private fun disposeTimer() {
        disposable?.isDisposed ?: disposable?.dispose()
    }

}



